<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>生成一个新的私钥</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.openssl-pkey-get-public.html">« openssl_pkey_get_public</a></li>
      <li style="float: right;"><a href="function.openssl-private-decrypt.html">openssl_private_decrypt »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.openssl.html">OpenSSL 函数</a></li>
    <li>生成一个新的私钥</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.openssl-pkey-new" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">openssl_pkey_new</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.2.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">openssl_pkey_new</span> &mdash; <span class="dc-title">生成一个新的私钥</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.openssl-pkey-new-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>openssl_pkey_new</strong></span>(<span class="methodparam"><span class="type">?</span><span class="type"><span class="type">array</span><span class="type"></span></span> <code class="parameter">$options</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><span class="type"><a href="class.opensslasymmetrickey.html" class="type OpenSSLAsymmetricKey">OpenSSLAsymmetricKey</a></span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>openssl_pkey_new()</strong></span> 生成一个新的私钥和公钥对。如何获取该密钥的公共组件参见以下示例。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
<span class="simpara">
 必须安装有效的 <var class="filename">openssl.cnf</var> 以保证此函数正确运行。参考有关<a href="openssl.installation.html" class="link">安装</a>的说明以获得更多信息。
</span>
</p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.openssl-pkey-new-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">options</code></dt>

     <dd>

      <p class="para">
       你可以使用 <code class="parameter">options</code> 参数微调密钥的生成（比如指定位数）。查看
       <span class="function"><a href="function.openssl-csr-new.html" class="function">openssl_csr_new()</a></span> 获取更多关于
       <code class="parameter">options</code> 的信息。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.openssl-pkey-new-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <span class="classname"><a href="class.opensslasymmetrickey.html" class="classname">OpenSSLAsymmetricKey</a></span> 实例，错误则返回 <strong><code>false</code></strong> 。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.openssl-pkey-new-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       成功时此函数会返回 <span class="classname"><a href="class.opensslasymmetrickey.html" class="classname">OpenSSLAsymmetricKey</a></span> 的实例；在之前版本中，则会返回类似为
       <code class="literal">OpenSSL key</code> 的 <a href="language.types.resource.html" class="link">资源(resource)</a>。
      </td>
     </tr>

     <tr>
      <td>7.1.0</td>
      <td>添加了 <code class="parameter">curve_name</code> 配置参数使得可以创建 EC 密钥。</td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.openssl-pkey-new-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="function.openssl-pkey-new.example.public-key">
   <p><strong>示例 #1 Obtain the public key from a private key</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$private_key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">openssl_pkey_new</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$public_key_pem&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">openssl_pkey_get_details</span><span style="color: #007700">(</span><span style="color: #0000BB">$private_key</span><span style="color: #007700">)[</span><span style="color: #DD0000">'key'</span><span style="color: #007700">];<br />echo&nbsp;</span><span style="color: #0000BB">$public_key_pem</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$public_key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">openssl_pkey_get_public</span><span style="color: #007700">(</span><span style="color: #0000BB">$public_key_pem</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$public_key</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArZFsmN2P6rx1Xt7YV95o
gcdlal0k3ryiIhFNzjwtRNNTXfEfBr6lUuaIJYQ8/XqEBX0hpcfuuF6tTRlonA3t
WLME0QFD93YVsAaXcy76YqjjqcRRodIBphAbYyyMI/lXkQAdn7kbAmr7neSOsMYJ
El9Wo4Hl4oG6e52ZnYHyqW9dxh4hX93eupR2TmcCdVf+r9xoHewP0KJYSHt7vDUX
AQlWYcQiWHIadFsmL0orr6mutlXFReoHbesgKY9/3YLOu0JfxflSjIZ2JeL1NTl1
MsmODsUwgAUrwnWKKx+eQUP5g3GnSB3dPkRh9zRVRiLNWbCugyjrf3e6DgQWrW7j
pwIDAQAB
-----END PUBLIC KEY-----
resource(5) of type (OpenSSL key)
</pre></div>
   </div>
  </div>
 </div>


</div></div></div></body></html>